﻿<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<!--
  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.

  Use of this source code is governed by a BSD-style license
  that can be found in the LICENSE file in the root of the source
  tree. An additional intellectual property rights grant can be found
  in the file PATENTS.  All contributing project authors may
  be found in the AUTHORS file in the root of the source tree.
-->
<html>
<head>
  <title>WebRTC PeerConnection Fuzz Test Template</title>
INCLUDE_RANDOM_JS
INCLUDE_FUZZ_SDP_JS
  <script type="text/javascript">
  var gFirstConnection = null;
  var gSecondConnection = null;

  // Variables in caps are filled in by the fuzzer.
  var gTransformOfferSdp = TRANSFORM_OFFER_SDP;
  var gTransformAnswerSdp = TRANSFORM_ANSWER_SDP;

  // START_OF_POSSIBLE_INJECTED_LOCATION_RELOADS
  function startTest() {
    navigator.webkitGetUserMedia(REQUEST_AUDIO_AND_VIDEO,
                                 getUserMediaOkCallback,
                                 getUserMediaFailedCallback);
  }

  function getUserMediaFailedCallback(error) {
    console.log('getUserMedia request failed with code ' + error.code);
  }

  function getUserMediaOkCallback(localStream) {
    var localStreamUrl = webkitURL.createObjectURL(localStream);
    document.getElementById('local-view').src = localStreamUrl;

    callUsingStream(localStream);
  }

  function callUsingStream(localStream) {
    gFirstConnection = new webkitRTCPeerConnection(null, null);
    gFirstConnection.onicecandidate = onIceCandidateToFirst;
    gFirstConnection.addStream(localStream);
    gFirstConnection.createOffer(onOfferCreated);
  }

  function onOfferCreated(offer) {
    gFirstConnection.setLocalDescription(offer);

    var offerSdp = gTransformOfferSdp(offer.sdp);
    console.log('OFFER SDP MESSAGE:' + offerSdp);
    receiveCall(offerSdp);
  }

  function receiveCall(offerSdp) {
    gSecondConnection = new webkitRTCPeerConnection(null, null);
    gSecondConnection.onicecandidate = onIceCandidateToSecond;
    gSecondConnection.onaddstream = onRemoteStream;

    var parsedOffer = new RTCSessionDescription({ type: 'offer',
                                                  sdp: offerSdp });
    gSecondConnection.setRemoteDescription(parsedOffer);

    gSecondConnection.createAnswer(onAnswerCreated);
  }

  function onAnswerCreated(answer) {
    gSecondConnection.setLocalDescription(answer);

    answerSdp = gTransformAnswerSdp(answer.sdp);
    console.log('ANSWER SDP MESSAGE:' + answerSdp);
    handleAnswer(answerSdp);
  }

  function handleAnswer(answerSdp) {
    var parsedAnswer = new RTCSessionDescription({ type: 'answer',
                                                   sdp: answerSdp });
    gFirstConnection.setRemoteDescription(parsedAnswer);
  }

  function onIceCandidateToFirst(event) {
    if (event.candidate) {
      var candidate = new RTCIceCandidate(event.candidate);
      gSecondConnection.addIceCandidate(candidate);
    }
  }

  function onIceCandidateToSecond(event) {
    if (event.candidate) {
      var candidate = new RTCIceCandidate(event.candidate);
      gFirstConnection.addIceCandidate(candidate);
    }
  }

  function onRemoteStream(e) {
    var remoteStreamUrl = webkitURL.createObjectURL(e.stream);
    document.getElementById('remote-view').src = remoteStreamUrl;
  }

  window.onload = function() {
    setRandomRolls(gRandomRolls);
    startTest();
  }
  // END_OF_POSSIBLE_INJECTED_LOCATION_RELOADS

  // This variable is placed here since its value is pretty big.
  var gRandomRolls = ARRAY_OF_RANDOM_ROLLS;
  </script>
</head>
<body>
  <table border="0">
    <tr>
      <td>Local Preview</td>
      <td>Remote Stream</td>
    </tr>
    <tr>
      <td><video width="320" height="240" id="local-view"
          autoplay="autoplay"></video></td>
      <td><video width="320" height="240" id="remote-view"
          autoplay="autoplay"></video></td>
    </tr>
  </table>
</body>
</html>